//
//  AXFFlashSupermarketViewController.m
//  aixinfeng005
//
//  Created by zhanghaifeng on 16/10/10.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

#import "AXFFlashSupermarketViewController.h"
#import "AXFSupermarketModel.h"
#import "AXFCategaryCellTableViewCell.h"
#import "AXFProductCell.h"
#import <YYModel.h>


static NSString *cell_left = @"left_Cell";

static NSString *cell_right = @"right_cell";

@interface AXFFlashSupermarketViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 *  左边的tableView
 */
@property(nonatomic,weak)UITableView * left_tbV;
/**
 *  右边的tableView
 */
@property(nonatomic,weak)UITableView * right_tbV;

/**
 *  左边的tableView数据
 */
@property(nonatomic,strong)NSArray * list_left;

@property(nonatomic,strong)NSMutableArray * right_Arr;

@property(nonatomic,strong)NSArray * keyArr;

@property(nonatomic,strong)NSMutableDictionary * animDict;

@end

@implementation AXFFlashSupermarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.animDict = [NSMutableDictionary dictionary];
    
    [self loadData];
    
    [self creatViews];
	
}

-(void)creatViews
{
    //MARK:- 添加左边的tbV
    UITableView *left_tbV = [[UITableView alloc] init];
    [self.view addSubview:left_tbV];
    [left_tbV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.top.equalTo(self.view);
        make.width.offset(80);
    }];
    //MARK:- 添加右边的tbV
    UITableView *right_tbV = [[UITableView alloc] init];
    [self.view addSubview:right_tbV];
    [right_tbV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.view);
        make.left.equalTo(left_tbV.mas_right);
        
    }];
    
    [left_tbV registerClass:[AXFCategaryCellTableViewCell class] forCellReuseIdentifier:cell_left];
//    UINib *nib = [UINib nibWithNibName:@"AXFProductCell" bundle:nil];
//    [right_tbV registerNib:nib forCellReuseIdentifier:cell_right];
    
    [right_tbV registerClass:[AXFProductCell class] forCellReuseIdentifier:cell_right];
    
    
    
    left_tbV.dataSource = self;
    left_tbV.delegate = self;
    right_tbV.dataSource = self;
    right_tbV.delegate = self;
    right_tbV.rowHeight = UITableViewAutomaticDimension;
    right_tbV.estimatedRowHeight = 100;
    self.left_tbV = left_tbV;
    self.right_tbV = right_tbV;
    
}
#pragma mark -- 实现数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.left_tbV) {
        return 1;
    }
    
    return self.list_left.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.left_tbV) {
        
        return self.list_left.count;
    }
    NSArray *arr = self.right_Arr[section];
    
    return arr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    

    if (tableView == self.left_tbV) {
        

        
          AXFCategaryCellTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cell_left forIndexPath:indexPath];
         AXFSupermarketModel * model = self.list_left[indexPath.row];
        
           cell.name = model.name;
        
        return  cell;
    }else
    {
        
        
       AXFProductCell  *cell = [tableView dequeueReusableCellWithIdentifier:cell_right forIndexPath:indexPath];
        
        AXFSupermarketModel * model = self.right_Arr[indexPath.section][indexPath.row];
        
        void(^animationBlock)(CGPoint point) = ^(CGPoint point){
            
            // - 1.创建图片框
            UIImageView *imgView = [[UIImageView alloc] init];
            imgView.frame = CGRectMake(0, 0, 50, 50);
            
            imgView.layer.cornerRadius = 25;
            imgView.layer.masksToBounds = YES;
            [imgView sd_setImageWithURL:[NSURL URLWithString:model.img]];
            
            imgView.center = point; // 点击加号按钮的位置!
            
            // - 获取主窗口
            UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
            [keyW addSubview:imgView];
            
            // MARK: - 2.添加动画效果! 关键帧核心动画
            // - 1.创建动画对象
            CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];

            
            // - 2.设置属性
            // - 2.1 设置路径
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:point]; // 起点
            
            // 参数1 结束点!
            // 参数2 控制点!
            CGPoint endP = CGPointMake(268, keyW.bounds.size.height);
            CGPoint controlP = CGPointMake(point.x -200, point.y - 200);
            [path addQuadCurveToPoint:endP controlPoint:controlP]; // 添加抛物线!
            
            [UIView animateWithDuration:0.6 animations:^{
                imgView.transform = CGAffineTransformScale(imgView.transform, 0.8, 0.8);
            } completion:^(BOOL finished) {
                [imgView removeFromSuperview];
            }];
            anim.path = path.CGPath;
            
            anim.duration = 0.7;

            
            // - 3.添加
            [imgView.layer addAnimation:anim forKey:nil];

        };
        
        cell.willAnimation = animationBlock;
        cell.data = model;
        return cell;
    }

    
}


#pragma mark -- 实现代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (tableView == self.left_tbV) {
        
        NSIndexPath *indePath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];

        [self.right_tbV scrollToRowAtIndexPath:indePath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.right_tbV) {
        if ((tableView.isDragging || tableView.isDecelerating || tableView.isTracking)) {
            
            NSIndexPath *indexP = [NSIndexPath indexPathForRow:indexPath.section inSection:0];
            
            [self.left_tbV selectRowAtIndexPath:indexP animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
    
    
}





-(void)loadData
{
    NSMutableDictionary *prama = [NSMutableDictionary dictionary];
    
    [prama setValue:@"5" forKey:@"call"];
    
    [DSHTTPClient postUrlString:@"supermarket.json.php" withParam:prama withSuccessBlock:^(NSDictionary* data) {
  //MARK:- 转为模型数据
       NSArray *left_DataArr = data[@"data"][@"categories"];
       NSDictionary *right_DataDict = data[@"data"][@"products"];
      
       self.list_left = [NSArray yy_modelArrayWithClass:[AXFSupermarketModel class] json:left_DataArr];
        self.right_Arr = [NSMutableArray array];

        for (AXFSupermarketModel *model in self.list_left) {
            NSArray *arr = [NSArray yy_modelArrayWithClass:[AXFSupermarketModel class] json: right_DataDict[model.id]];
            
            [self.right_Arr addObject:arr];
            
        }
        [self.left_tbV reloadData];
        [self.right_tbV reloadData];
        
    } withFailedBlock:^(NSError *error) {
        
    } withErrorBlock:^(NSString *message) {
        
    }];
    
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
